package com.wxmp.core.util;

import com.wxmp.wxcms.ctrl.UserCalcCtrl;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
 * @author 孔繁玉
 * @date 2018年9月9日 上午9:25:18
 * @explain 根据经纬度获取地址 
 */
public class MyGetLocation {
    private static Logger logger=Logger.getLogger(UserCalcCtrl.class);
 public static void main(String[] args){
  String res = getAdd("34.763","113.65");
  System.out.println("==="+res);
 }
 /**
  * 阿里云根据经纬度获取地区名接口：
  * http://gc.ditu.aliyun.com/regeocoding?l=39.938133,116.395739&type=001
  * 阿里云根据地区名获取经纬度接口：
  * http://gc.ditu.aliyun.com/geocoding?a=苏州市
  * 河南郑州经纬度 经度：113.65 ， 纬度：34.76
  * @param lat 小：经度
  * @param log 大: 纬度
  * @return
  */
 public static String getAdd(String lat,String log) {
  /** type(100代表道路，010代表POI，001代表门址，111前面都是)   */
  String urlString = "http://gc.ditu.aliyun.com/regeocoding?l="+lat+","+log+"&type=010";
  String res = "";
  try {
   URL url = new URL(urlString);
   HttpURLConnection conn = (HttpURLConnection) url.openConnection();
   conn.setDoInput(true);
   conn.setRequestMethod("POST");
   BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));
   String line;
   while((line = in.readLine())!= null) {
    res += line+"\n";
   }
   in.close();
  
  } catch (Exception e) {
      logger.debug("查询经纬度出错: "+e.getMessage());
  }
  System.out.println(res);
  JSONObject jsonObject = JSONObject.fromObject(res);
  JSONArray jsonArray = JSONArray.fromObject(jsonObject.getString("addrList"));
  JSONObject addrObj = JSONObject.fromObject(jsonArray.get(0));
  String addname = addrObj.getString("admName") !=null ? addrObj.getString("admName") : "";
  return addname;
 }
}

